package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.support.CsunMessageSource;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsRecord;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.vo.FeeVo;
import com.csun.dsms.model.vo.ServiceRecordVo;
import com.csun.dsms.model.vo.YearVo;
import com.csun.dsms.model.vo.poor.PoorNumberVo;
import com.csun.dsms.model.dto.record.RecordQueryDto;
import com.csun.dsms.model.vo.staff.StaffNumberVo;
import com.csun.dsms.service.*;
import com.csun.dsms.utils.DateUtil;
import com.google.common.collect.Maps;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-29 14:12
 * @description: 统计分析拦截器
 */
@RestController
@RequestMapping("/analyze")
public class DsmsAnalyzeController {

    @Resource
    private CsunMessageSource csunMessageSource;
    @Resource
    private DsmsPoorService dsmsPoorService;
    @Resource
    private DsmsStaffService dsmsStaffService;
    @Resource
    private DsmsRecordService dsmsRecordService;
    @Resource
    private DsmsServiceRecordService dsmsServiceRecordService;
    @Resource
    private DsmsFeeService dsmsFeeService;

    /**
     * 获取首页统计数据
     *
     *
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/index")
    public Wrapper index() {

        Map<String, Object> result = Maps.newHashMap();

        String year = DateUtil.getNowYear();

        // 获取特困人员数量村
        List<PoorNumberVo> poorNumberVoListVillage = dsmsPoorService.queryPoorNumberListByVillage(year);
        // 获取特困人员数量镇
        List<PoorNumberVo> poorNumberVoList = dsmsPoorService.queryPoorNumberList(year);
        int poorTotal = dsmsPoorService.queryPoorTotal(year);
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        for (PoorNumberVo poorNumberVo : poorNumberVoList) {
            poorNumberVo.setPercent(decimalFormat.format((float)poorNumberVo.getNumber()/poorTotal) + "%");
        }
        for (PoorNumberVo poorNumberVo : poorNumberVoListVillage) {
            poorNumberVo.setPercent(decimalFormat.format((float)poorNumberVo.getNumber()/poorTotal) + "%");
        }
        Map<String, Object> poorNumberMap = Maps.newHashMap();
        poorNumberMap.put("poorNumberVoListVillage", poorNumberVoListVillage);
        poorNumberMap.put("poorNumberVoList", poorNumberVoList);
        poorNumberMap.put("poorTotal", poorTotal);
        // 获取服务人员数量
        List<Map<String,Integer>> staffNumberVoList = dsmsStaffService.queryStaffNumberByMonth(year);
        List<StaffNumberVo> staffNumberVos = new ArrayList<>();
        for (Map staff:staffNumberVoList) {
            StaffNumberVo staffNumberVo = new StaffNumberVo();
            Set<String> set = staff.keySet();
            for (String s:set) {
                Integer i = Integer.valueOf(s.substring(5))-1;
                String month = i.toString();
                staffNumberVo.setMonth(month);
                staffNumberVo.setNumber(Integer.valueOf(staff.get(s).toString()));
                staffNumberVos.add(staffNumberVo);
                staffNumberVo = new StaffNumberVo();
            }
        }
        Map<String, Object> staffNumberMap = Maps.newHashMap();
        staffNumberMap.put("staffNumberVoList", staffNumberVos);
        // 获取服务记录
        RecordQueryDto recordQueryDto = new RecordQueryDto();
        recordQueryDto.setPageSize(100);
        recordQueryDto.setYear(year);
        List<DsmsRecord> dsmsRecordList = dsmsRecordService.queryListWithPage(recordQueryDto);
        //查询服务项目占比
        List<ServiceRecordVo> serviceRecordVos = dsmsServiceRecordService.serviceQueryByProject();
        //查询结算金额
        FeeVo feeVo = new FeeVo();
        feeVo.setServiceTime(year);
        List<FeeVo> feeVos = dsmsFeeService.feeQueryByMonth(feeVo);
        result.put("poorNumberMap", poorNumberMap);
        result.put("staffNumberMap", staffNumberMap);
        result.put("dsmsRecordList", dsmsRecordList);
        result.put("serviceRecordVos",serviceRecordVos);
        result.put("feeVos",feeVos);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 获取每年结算金额
     *
     * @param login the login user
     *
     * @return  the wrapper.
     */
    @Authorization
    @PostMapping(value = "/getFeeByYear")
    public Wrapper getFeeByYear(@CurrentUser DsmsUser login, String year) {

        FeeVo feeVo = new FeeVo();
        feeVo.setServiceTime(year);
        List<FeeVo> feeVos = dsmsFeeService.feeQueryByMonth(feeVo);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), feeVos);
    }

    /**
     * 获取每年每个地方的特困人员数量
     *
     * @param login the login user
     *
     * @return  the wrapper.
     */
    @Authorization
    @PostMapping(value = "/getPoorNumber")
    public Wrapper getPoorNumber(@CurrentUser DsmsUser login, String year) {
        // 获取特困人员数量村
        List<PoorNumberVo> poorNumberVoListVillage = dsmsPoorService.queryPoorNumberListByVillage(year);
        // 获取特困人员数量镇
        List<PoorNumberVo> poorNumberVoList = dsmsPoorService.queryPoorNumberList(year);
        int poorTotal = dsmsPoorService.queryPoorTotal(year);
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        for (PoorNumberVo poorNumberVo : poorNumberVoListVillage) {
            poorNumberVo.setPercent(decimalFormat.format((float)poorNumberVo.getNumber()/poorTotal) + "%");
        }
        for (PoorNumberVo poorNumberVo : poorNumberVoList) {
            poorNumberVo.setPercent(decimalFormat.format((float)poorNumberVo.getNumber()/poorTotal) + "%");
        }
        Map<String, Object> poorNumberMap = Maps.newHashMap();
        poorNumberMap.put("poorNumberVoList", poorNumberVoList);
        poorNumberMap.put("poorNumberVoListVillage", poorNumberVoListVillage);
        poorNumberMap.put("poorTotal", poorTotal);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), poorNumberMap);
    }

    /**
     * 获取每年每个月的服务人员数量
     *
     * @param 
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/getStaffNumber")
    public Wrapper getStaffNumber( @RequestBody YearVo yearVo) {
        String year = yearVo.getYear();
        // 获取服务人员数量
        List<Map<String,Integer>> staffNumberVoList = dsmsStaffService.queryStaffNumberByMonth(year);
        List<StaffNumberVo> staffNumberVos = new ArrayList<>();
        for (Map staff:staffNumberVoList) {
            StaffNumberVo staffNumberVo = new StaffNumberVo();
            Set<String> set = staff.keySet();
            for (String s:set) {
                Integer i = Integer.valueOf(s.substring(5))-1;
                String month = i.toString();
                staffNumberVo.setMonth(month);
                staffNumberVo.setNumber(Integer.valueOf(staff.get(s).toString()));
                staffNumberVos.add(staffNumberVo);
                staffNumberVo = new StaffNumberVo();
            }

        }
        Map<String, Object> staffNumberMap = Maps.newHashMap();
        staffNumberMap.put("staffNumberVoList", staffNumberVos);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), staffNumberMap);
    }
}
